package cn.syc.struck.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import cn.syc.struck.demo.adapter.MyAdapter;
import cn.syc.struck.demo.base.BaseActivity;
import cn.syc.struck.demo.http.entity.gen.BlogPager;
import cn.syc.struck.demo.http.service.BlogService;
import cn.syc.struck.demo.http.subscribers.ProgressSubscriber;
import cn.syc.struck.demo.http.subscribers.SubscriberOnNextListener;

public class CollapsingToolbarLayoutActivity extends BaseActivity {
    public static final String CLASS_TAG = "LinearActivity";

    private XRecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private ArrayList<String> listData;

    private View mEmptyView;
    //当前页码
    int curpage = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collapsing_toolbar_layout;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mRecyclerView = (XRecyclerView) this.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        //----------------
        mEmptyView = findViewById(R.id.include_view_empty);
        mRecyclerView.setEmptyView(mEmptyView);

        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                SubscriberOnNextListener callback = new SubscriberOnNextListener<BlogPager>() {
                    @Override
                    public void onNext(BlogPager t) {
                        curpage = 1;
                        listData.clear();
                        if (t.getTotalRow() > 0) {
                            for (BlogPager.ListBean b : t.getList()) {
                                String txt = String.format("%02d.%s\r\n%s", b.getId(), b.getTitle(), b.getContent());
                                listData.add(txt);
                            }
                            mAdapter.notifyDataSetChanged();
                            mRecyclerView.refreshComplete();
                            if (t.getTotalPage() <= 1)
                                mRecyclerView.setNoMore(true);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                };

                BlogService.getInstance().getBlogs(1, new ProgressSubscriber(callback, CollapsingToolbarLayoutActivity.this, CLASS_TAG));
            }

            @Override
            public void onLoadMore() {

                SubscriberOnNextListener callback = new SubscriberOnNextListener<BlogPager>() {
                    @Override
                    public void onNext(BlogPager t) {
                        for (BlogPager.ListBean b : t.getList()) {
                            String txt = String.format("%02d.%s\r\n%s", b.getId(), b.getTitle(), b.getContent());
                            listData.add(txt);
                        }
                        mAdapter.notifyDataSetChanged();
                        mRecyclerView.refreshComplete();
                        if (t.isLastPage()) {
                            mRecyclerView.setNoMore(true);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                };

                //++curpage,加之后再用值，则为第二页开始
                BlogService.getInstance().getBlogs(++curpage, new ProgressSubscriber(callback, CollapsingToolbarLayoutActivity.this, CLASS_TAG));

            }
        });

        listData = new ArrayList<String>();
        mAdapter = new MyAdapter(listData);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();
        //========================
        findViewById(R.id.button_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //相当于重新下拉刷新
                mRecyclerView.refresh();
                mRecyclerView.refreshComplete();
            }
        });
        //---------------------
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
